<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="知识点" prop="konwlegename">
        <el-input v-model="queryParams.konwlegename" placeholder="请输入知识点"  size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['receipt:exampleSubjectAnalysisAudit:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['receipt:exampleSubjectAnalysisAudit:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['receipt:exampleSubjectAnalysisAudit:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="exampleSubjectAnalysisAuditList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false" />
      <el-table-column label="解析内容" align="center" prop="content" v-if="false" />
      <el-table-column label="tb_subject_manager表id" align="center" prop="subjectManagerId" v-if="false" />
      <el-table-column label="知识点" align="center" prop="zsName" />
      <el-table-column label="例题" align="center" prop="clusteName" />
      <el-table-column label="顺序" align="center" prop="sort" />
      <el-table-column label="处理状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="删除状态" align="center" prop="isDel" :formatter="isDelFormat" v-if="false" />
      <el-table-column label="审核意见" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div v-if="scope.row.status == '1'" style="display: inline;">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="AnalysisSonsent(scope.row)"
              v-hasPermi="['receipt:exampleSubjectAnalysisAudit:Consent']">通过</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="AnalysisReject(scope.row)"
              v-hasPermi="['receipt:exampleSubjectAnalysisAudit:Reject']">驳回</el-button>
          </div>
          <div style="display: inline;">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['receipt:exampleSubjectAnalysisAudit:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['receipt:exampleSubjectAnalysisAudit:remove']">查看</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 例题列表表单 -->
    <el-dialog :title="title1" :visible.sync="dialogVisible" width="500px" append-to-body>
      <div>
        <el-input v-model="cname" placeholder="请输入标题" clearable size="small" style="width:55%"></el-input>
        <el-button @click="czs()" style="margin-left: 15px;">搜索</el-button>
      </div>
      <el-table :data="clusteAuditList" @selection-change="handleSelectionChange1" @select="selectChange"
        ref="multipleTable" @row-click="handleRowClick" @select-all="selectAll" style="margin-top: 15px;">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" v-if="false" />
        <el-table-column label="例题" align="center" prop="title" />
      </el-table>
      <pagination v-show="total>0" :total="total2" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="Cluste" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormT()">确 定</el-button>
        <el-button @click="cancelT">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 知识点列表表单 -->
    <el-dialog :title="title2" :visible.sync="ZSVisible" width="500px" append-to-body>
      <div>
        <el-input v-model="name" placeholder="请输入标题" clearable size="small" style="width:55%"></el-input>
        <el-button @click="zs()" style="margin-left: 15px;">搜索</el-button>
      </div>
      <el-table :data="ZSAuditList" @selection-change="handleSelectionChange2" @select="selectChange2"
        ref="multipleTable" @row-click="handleRowClick2" @select-all="selectAll2" style="margin-top: 15px;">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" v-if="false" />
        <el-table-column label="知识点" align="center" prop="title" />
      </el-table>
      <pagination v-show="total>0" :total="total1" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="ClusteZ" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormZ()">确 定</el-button>
        <el-button @click="cancelZ">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title1" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核意见" prop="remark">
          <el-input v-model="remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改例题解析审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="解析内容" prop="content">
          <editor v-model="form.content" :editor-class="'height:192px;'" />
        </el-form-item>
        <el-form-item label="例题" prop="subjectClusteId">
          <el-button @click="Cluste">例题</el-button>
          <el-input v-model="this.idscluste" :disabled="true" v-if="false"></el-input>
          <el-input v-model="this.clusteName" :disabled="true" style="width: 30%; margin-left: 15px"></el-input>
        </el-form-item>
        <el-form-item label="知识类型" prop="knowledgeManagerId">
          <el-button @click="ClusteZ">知识点</el-button>
          <el-input v-model="this.knowledgeids" :disabled="true" v-if="false"></el-input>
          <el-input v-model="this.knowledgeManagerName" :disabled="true" style="width: 40%; margin-left: 10px"></el-input>
        </el-form-item>
        <el-form-item label="顺序" prop="sort">
          <el-input-number v-model="form.sort" placeholder="请输入顺序" controls-position="right" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isshow">确 定</el-button>
        <el-button @click="cancel" v-if="isshow">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    listExampleSubjectAnalysisAudit,
    getExampleSubjectAnalysisAudit,
    delExampleSubjectAnalysisAudit,
    addExampleSubjectAnalysisAudit,
    updateExampleSubjectAnalysisAudit,
    exportExampleSubjectAnalysisAudit,
    AnalysisConsent,
    Analysiseject,
    selectClName,
    konwlegName,
    selectManageList
  } from "@/api/receipt/exampleSubjectAnalysisAudit";
  import Editor from '@/components/Editor';
  import {
    listManagerL,
    selectManageStatus
  } from "@/api/receipt/exampleSubjectManage";
  import {
    listManager,
    selectKmName,
    selectKnowledge
  } from "@/api/knowledge/knowledgeManager";
  export default {
    name: "ExampleSubjectAnalysisAudit",
    components: {
      Editor,
    },
    data() {
      var checkAge = (rule, knowledgeManagerId, callback) => {
        if (this.knowledgeids == null) {
          return callback(new Error('请选择知识点'));
        } else {
          callback();
        }
      };
      var checkAgecluster = (rule, subjectClusteId, callback) => {
        if (this.idscluste == null) {
          return callback(new Error('请选择题簇'));
        } else {
          callback();
        }
      };
      var sort = (rule, value, callback) => {
        if (value > 100) {
          return callback(new Error('顺序不能大于100'));
        } else if (value <= 0) {
          return callback(new Error('顺序不能小于1'));
        } else if (Number(value) + '' == 'NaN') {
          return callback(new Error('顺序个数必须为数字'));
        } else {
          callback();
        }
      };
      return {
        title1: "例题选择",
        title2: "知识点选择",
        isshow: true,
        clusteAuditList: [],
        ZSAuditList: [],
        name: null,
        cname: null,
        status: null,
        remark: null,
        id: null,
        idscluste: null,
        clusteName: null,
        knowledgeManagerName: "",
        knowledgeids: "",
        ZSVisible: false,
        dialogVisible: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        total1: 0,
        total2: 0,
        // 例题解析审核表格数据
        exampleSubjectAnalysisAuditList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open1: false,
        // 处理状态 1未审核  2已审核 3驳回字典
        statusOptions: [],
        // 删除状态 0未删除(默认) 1已删除字典
        isDelOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          konwlegename: null,
          content: null,
          subjectManagerId: null,
          knowledgeManagerRelated: null,
          sort: null,
          status: null,
          isDel: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          content: [{
            required: true,
            message: "请输入内容",
            trigger: "blur"
          }],
          sort: [{
              required: true, //是否必填
              message: '顺序不能为空', //规则
              trigger: 'blur' //何事件触发
            },
            {
              validator: sort,
              trigger: 'blur'
            },
          ],
          subjectClusteId: [{
            validator: checkAgecluster,
            trigger: "blur"
          }],
          knowledgeManagerId: [{
            validator: checkAge,
            trigger: 'blur'
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_audit_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("sys_delete_status").then(response => {
        this.isDelOptions = response.data;
      });
    },
    methods: {
      /** 查询例题解析审核列表 */
      getList() {
        this.loading = true;
        listExampleSubjectAnalysisAudit(this.queryParams).then(response => {
          this.exampleSubjectAnalysisAuditList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 处理状态 1未审核  2已审核 3驳回字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 删除状态 0未删除(默认) 1已删除字典翻译
      isDelFormat(row, column) {
        return this.selectDictLabel(this.isDelOptions, row.isDel);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.idscluste = null;
        this.knowledgeids = "";
        this.knowledgeManagerName = "";
        this.clusteName = null;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          knowledgeManagerName: null,
          id: null,
          content: null,
          subjectManagerId: null,
          knowledgeManagerRelated: null,
          sort: null,
          status: null,
          isDel: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        if (this.queryParams.konwlegename == null && this.queryParams.status != null) {
          this.queryParams.pageNum = 1;
          this.getList();
        }
        if (this.queryParams.konwlegename == null && this.queryParams.status == null) {
          this.queryParams.pageNum = 1;
          this.getList();
        }
        if (this.queryParams.konwlegename != null && this.queryParams.status != null) {
          this.$confirm('该模块只能选择一个查询条件', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          });
        }
        if (this.queryParams.konwlegename != null && this.queryParams.status == null) {
          this.queryParams.pageNum = 1;
          alert(this.queryParams.konwlegename)
          konwlegName(this.queryParams.konwlegename).then(response => {
            this.exampleSubjectAnalysisAuditList = response;
            this.total = response.total;
            this.loading = false;
          });
        }
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加例题解析审核";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getExampleSubjectAnalysisAudit(id).then(response => {
          //alert(JSON.stringify(response))
          this.form = response.data;
          this.idscluste=response.data.subjectManagerId;
          this.clusteName = response.data.clusteName;
          this.knowledgeManagerName = response.data.zsName;
          this.isshow = true;
          this.open = true;
          this.title = "修改例题解析审核";
        });
      },
      zs() {
        if (this.name.length == 0) {
          this.$confirm('请先输入搜索内容', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          });
        } else {
          selectKmName(this.name).then(response => {
            this.ZSVisible = false;
            this.ZSAuditList = response;
            this.ZSVisible = true;
          });
        }
      },
      czs() {
        if (this.cname.length == 0) {
          this.$confirm('请先输入搜索内容', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          });
        } else {
          selectClName(this.cname).then(response => {
            this.dialogVisible = false;
            this.clusteAuditList = response;
            this.dialogVisible = true;
          });
        }
      },
      submitFormT() {
        this.dialogVisible = false;
      },
      cancelT() {
        this.dialogVisible = false;
      },
      submitFormZ() {
        this.ZSVisible = false;
      },
      cancelZ() {
        this.ZSVisible = false;
      },
      ClusteZ() {
        selectKnowledge(this.queryParams).then(response => {
          this.total1 = response.total;
          this.ZSAuditList = response.rows;
          this.ZSVisible = true;
        });
      },
      Cluste() {
        selectManageList(this.queryParams).then(response => {
         
          this.total2 = response.total;
          this.clusteAuditList = response.rows;
          this.dialogVisible = true;
        });
      },
      handleSelectionChange1(selection) {
        this.idscluste = selection[0].id
        this.clusteName = selection[0].title

      },
      selectChange(selection, row) {
        if (selection.length > 1) {
          const del_row = selection.shift()
          this.$refs.multipleTable.toggleRowSelection(del_row, false)
        }
      },
      // 点击行触发，选中或不选中复选框
      handleRowClick(row, column, event) {
        this.$refs.multipleTable.toggleRowSelection(row)
        this.selectChange(this.handleSelectionList)
      },
      selectAll(selection) {
        if (selection.length > 1) {
          selection.length = 1
        }
      },
      handleSelectionChange2(selection) {
        this.knowledgeids = selection[0].id
        this.knowledgeManagerName = selection[0].title

      },
      selectChange2(selection, row) {
        if (selection.length > 1) {
          const del_row = selection.shift()
          this.$refs.multipleTable.toggleRowSelection(del_row, false)
        }
      },
      // 点击行触发，选中或不选中复选框
      handleRowClick2(row, column, event) {
        this.$refs.multipleTable.toggleRowSelection(row)
        this.selectChange(this.handleSelectionList)
      },
      selectAll2(selection) {
        if (selection.length > 1) {
          selection.length = 1
        }
      },
      /** 同意按钮操作 */
      AnalysisSonsent(row) {
        this.open1 = true;
        this.id = row.id;
        //alert(this.id)
        this.status = 2;
      },
      AnalysisReject(row) {
        this.open1 = true;
        this.id = row.id;
        this.status = 3;
      },
      cancel1() {
        this.open1 = false;
      },
      submitForm1() {
        this.form.id = this.id;
        this.form.remark = this.remark
        if (this.status == 2) {
          this.form.status = this.status;
          AnalysisConsent(this.form).then(response => {
            this.msgSuccess("审批已通过");
            this.remark = null;
            this.open1 = false;
            this.getList();
          });
        } else {
          this.form.status = this.status;
          Analysiseject(this.form).then(respon => {
            this.msgSuccess("审批已驳回");
            this.remark = null;
            this.open1 = false;
            this.getList();
          });
        }
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateExampleSubjectAnalysisAudit(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.idscluste = null;
                this.knowledgeids = "";
                this.knowledgeManagerName = "";
                this.clusteName = null;
                this.getList();
              });
            } else {
              this.form.subjectManagerId = this.idscluste;
              this.form.knowledgeManagerRelated = this.knowledgeids;
              addExampleSubjectAnalysisAudit(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.idscluste = null;
                this.knowledgeids = "";
                this.knowledgeManagerName = "";
                this.clusteName = null;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.reset();
        const id = row.id || this.ids
        getExampleSubjectAnalysisAudit(id).then(response => {
          this.form = response.data;
          this.clusteName = response.data.clusteName;
          this.knowledgeManagerName = response.data.zsName;
          this.isshow = false;
          this.open = true;
          this.title = "查看例题解析";
        });
      },
    }
  };
</script>
